<?php
class M_managebook extends CI_Model{
	function data_managebook($key){
		$data = array();
		$kuery = "SELECT m_produk.*, kategori_nama
			FROM m_produk
			LEFT JOIN m_kategori ON produk_kategori = kategori_id
		";
		if($key !== ""){
			$kuery .= " WHERE LOWER(m_produk.produk_nama) LIKE '%" . strtolower($key) . "%'";
		}
		$query = mysql_query($kuery);
		$num_rows = mysql_num_rows($query);
		for($a=0;$a<$num_rows;$a++){
			$fetch = mysql_fetch_array($query);
			$data[$a]=$fetch;
		}
		$result = json_encode($data);
		return $result;
	}
	function data_kategori(){
		$data = array();
		$kuery = "SELECT * FROM m_kategori";
		$query = mysql_query($kuery);
		$num_rows = mysql_num_rows($query);
		for($a=0;$a<$num_rows;$a++){
			$fetch = mysql_fetch_array($query);
			$data[$a]=$fetch;
		}
		$result = json_encode($data);
		return $result;
	}
	function managebook_add($managebook_kode,$managebook_nama,$managebook_kategori,
			$managebook_deskripsi,$managebook_harga,$managebook_sampul,$managebook_file,$managebook_aktif){
		$data = array(
			'produk_kode' => $managebook_kode,
			'produk_nama' => $managebook_nama,
			'produk_kategori' => $managebook_kategori,
			'produk_deskripsi' => $managebook_deskripsi,
			'produk_harga' => $managebook_harga,
			'produk_sampul' => $managebook_sampul,
			'produk_file' => $managebook_file,
			'produk_aktif' => $managebook_aktif
		);
		$this->db->insert('m_produk', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function managebook_edit($managebook_id,$managebook_kode,$managebook_nama,$managebook_kategori,
			$managebook_deskripsi,$managebook_harga,$managebook_sampul,$managebook_file,$managebook_aktif){
		$data = array(
			'produk_kode' => $managebook_kode,
			'produk_nama' => $managebook_nama,
			'produk_deskripsi' => $managebook_deskripsi,
			'produk_harga' => $managebook_harga,
			'produk_aktif' => $managebook_aktif
		);
		$kuery_check = "SELECT kategori_id FROM m_kategori WHERE kategori_id = '" . $managebook_kategori . "'";
		$query_check = $this->db->query($kuery_check);
		if($query_check->num_rows() > 0){
			$data['produk_kategori'] = $managebook_kategori;
		}
		if($managebook_sampul !== ""){
			$data['produk_sampul'] = $managebook_sampul;
		}
		if($managebook_sampul !== ""){
			$data['produk_file'] = $managebook_file;
		}
		$this->db->where('produk_id', $managebook_id);
		$this->db->update('m_produk', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function managebook_delete($managebook_id){
		$kuery = "DELETE FROM m_produk WHERE produk_id = '" . $managebook_id . "'";
		$query = mysql_query($kuery);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
}
?>